package cn.tedu.charging.order.producer;

import cn.tedu.charging.order.serializer.MessageSerializer;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MqttProducer<T> {
    @Autowired
    private MqttClient mqttClient;
    @Autowired
    private MessageSerializer messageSerializer;

    public void send(String topic,T msg,int qos,boolean retained){
        byte[] pa = messageSerializer.serialize(msg);

        try{
            mqttClient.publish(topic,pa,qos,retained);
        }catch (Exception e){
            log.error("发送消息失败",e);
        }
    }

    public void send(String topic,T msg){
        send(topic,msg,0,true);
    }
}
